
//接收 Date()
//转换 xxxx-xx-xx xx:xx:xx 格式
export const timeTrans = (now) => {
  let year = now.getFullYear();       //年
  let month = now.getMonth() + 1;     //月
  let day = now.getDate();            //日

  let hh = now.getHours();            //时
  let mm = now.getMinutes();          //分
  let ss = now.getSeconds();          //秒

  let clock = year + "-";
  if (month < 10)
    clock += "0";
  clock += month + "-";
  if (day < 10)
    clock += "0";
  clock += day + " ";
  if (hh < 10)
    clock += "0";
  clock += hh + ":";
  if (mm < 10)
    clock += '0';
  clock += mm + ":";
  if (ss < 10)
    clock += '0';
  clock += ss;
  return clock

}

export const timeTransYmd = (now) => {
  let year = now.getFullYear();       //年
  let month = now.getMonth() + 1;     //月
  let day = now.getDate();            //日

  let clock = year + "-";
  if (month < 10)
    clock += "0";
  clock += month + "-";
  if (day < 10)
    clock += "0";
  clock += day + " ";
  return clock

}

export const todayZeroTime = () => {
  let now = new Date();
  let year = now.getFullYear();       //年
  let month = now.getMonth() + 1;     //月
  let day = now.getDate();            //日

  let clock = year + "-";
  if (month < 10)
    clock += "0";
  clock += month + "-";
  if (day < 10)
    clock += "0";
  clock += day + " ";
  clock += "00:00:00"
  return clock
}

export const tomorrowZeroTime = () => {
  let now = new Date();
  let year = now.getFullYear();       //年
  let month = now.getMonth() + 1;     //月
  let day = now.getDate() + 1;        //日

  let clock = year + "-";
  if (month < 10)
    clock += "0";
  clock += month + "-";
  if (day < 10)
    clock += "0";
  clock += day + " ";
  clock += "00:00:00"
  return clock
}

export const ZeroTime = (now) => {
  let year = now.getFullYear();       //年
  let month = now.getMonth() + 1;     //月
  let day = now.getDate();            //日

  let clock = year + "-";
  if (month < 10)
    clock += "0";
  clock += month + "-";
  if (day < 10)
    clock += "0";
  clock += day + " ";
  clock += "00:00:00"
  return clock
}

export const  formatDate = (str) => {
  let date = new Date(str);
  let year = date.getFullYear();
  let month= date.getMonth() + 1;
  month= month< 10 ? ('0' + month) : month;
  let day = date.getDate();
  day = day < 10 ? ('0' + day ) : day ;
  let h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  let m = date.getMinutes();
  m = m < 10 ? ('0' + m) : m;
  let s = date.getSeconds();
  s = s < 10 ? ('0' + s) : s;
  return year + '-' + month + '-' + day + ' ' + h + ':' + m + ':' + s;
}

// 时间间隔秒
export const twoTimeInterval=(startTime, endTime)=> {
  // 开始时间
  let d1 = startTime.replace(/\-/g, "/");
  let date1 = new Date(d1);

  // 结束时间
  let d2 = endTime.replace(/\-/g, "/");
  let date2 = new Date(d2);

  // 时间相差秒数
  let dateDiff = date2.getTime() - date1.getTime();

  // 计算出相差天数
  let days = Math.floor(dateDiff / (24 * 3600 * 1000));

  // 计算出小时数
  let residue1 = dateDiff % (24 * 3600 * 1000); // 计算天数后剩余的毫秒数
  let hours = Math.floor(residue1 / (3600 * 1000));

  // 计算相差分钟数
  let residue2 = residue1 % (3600 * 1000); // 计算小时数后剩余的毫秒数
  let minutes = Math.floor(residue2 / (60 * 1000));

  // 计算相差秒数
  let residue3 = residue2 % (60 * 1000); // 计算分钟数后剩余的毫秒数
  let seconds = Math.round(residue3 / 1000);

  let returnVal = seconds + (minutes*60) + (hours*60*60) + (days*24*60*60)

  return returnVal;
}

// 时间间隔天时分秒
export const timeIntervalSFM=(startTime, endTime)=> {
  // 开始时间
  let d1 = startTime.replace(/\-/g, "/");
  let date1 = new Date(d1);

  // 结束时间
  let d2 = endTime.replace(/\-/g, "/");
  let date2 = new Date(d2);

  // 时间相差秒数
  let dateDiff = date2.getTime() - date1.getTime();

  // 计算出相差天数
  let days = Math.floor(dateDiff / (24 * 3600 * 1000));

  // 计算出小时数
  let residue1 = dateDiff % (24 * 3600 * 1000); // 计算天数后剩余的毫秒数
  let hours = Math.floor(residue1 / (3600 * 1000));

  // 计算相差分钟数
  let residue2 = residue1 % (3600 * 1000); // 计算小时数后剩余的毫秒数
  let minutes = Math.floor(residue2 / (60 * 1000));

  // 计算相差秒数
  let residue3 = residue2 % (60 * 1000); // 计算分钟数后剩余的毫秒数
  let seconds = Math.round(residue3 / 1000);

  let returnVal =
    ((days == 0) ? "" : days+"天") +
    ((hours == 0) ? "" : days+"时") +
    ((minutes == 0) ? "" : minutes+"分") +
    ((seconds == 0) ? "" : seconds+"秒");

  return returnVal;
}
